package wh.coach._collection;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;

/**
 * @author hangwen
 * @date 2023/6/25
 */
public class _ArrayList {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //ArrayList 基于数组的实现
        List<String> strList = new ArrayList<>();

        //添加
        strList.add("a");
        strList.add("b");
        strList.add("c");

        for (String s : strList) {
            System.out.println(s);
        }

        //should be "a"
        final String s = strList.get(0);

        //删除
        System.out.println("strList remove index:0");
        strList.remove(0);
        System.out.println(strList);

        System.out.println("strList remove item:c");
        strList.remove("c");
        System.out.println(strList);

        //修改
        System.out.println("set 0:B");
        strList.set(0, "B");
        System.out.println(strList);

        System.out.println("set 1 :B,will throw exception");
//        strList.set(1,"B");
//        System.out.println(strList);

        //list的泛型只能是引用类型
        List<Integer> intList = new ArrayList<>();

        intList.add(1);//隐式装箱
        intList.add(2);
        intList.add(3);

        //删除哪一个？
        intList.remove(2);
        System.out.println(intList);

        //问题：如何删除strList中的大写字母
        strList = new ArrayList<>();
        for (int i = 65; i < 91; i++) {
            strList.add((char) i + "");
            strList.add((char) (i + 32) + "");
        }
        System.out.println(strList);

        System.out.println("删除大写字母");
        //写法1
//        for (int i = 0; i < strList.size(); i++) {
//            String str = strList.get(i);
//            if (str.charAt(0) < 97) {
//                strList.remove(i);
//            }
//        }

        //写法2 错误 会报错 在迭代一个集合的过程中，不可增删其元素
//        for (String str : strList) {
//            if (str.charAt(0) < 97) {
//                strList.remove(str);
//            }
//        }

        //写法3
//        List<String> toRemove = new ArrayList<>();
//        for (String str : strList) {
//            if (str.charAt(0) < 97) {
//                toRemove.add(str);
//            }
//        }
//
//        for (String str : toRemove) {
//            strList.remove(str);
//        }

        //写法4 跌代器删除
        strList.removeIf(str -> str.charAt(0) < 97);

        System.out.println(strList);

        //扩容
        final Field field = ArrayList.class.getDeclaredField("elementData");
        field.setAccessible(true);

        strList = new ArrayList<>();

        for (int i = 65; i < 91; i++) {
            strList.add((char) i + "");
            Object elementData = field.get(strList);
            final int capacity = Array.getLength(elementData);

            System.out.println(String.format("strList size:%s,capacity:%s", strList.size(), capacity));
        }

        //guava
        strList = Lists.newArrayListWithExpectedSize(26);
        Object elementData = field.get(strList);
        System.out.println(String.format("guava strList size:%s,capacity:%s", strList.size(), Array.getLength(elementData)));

    }
}
